﻿/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is AST'r Framework.
  
  The Initial Developer of the Original Code is
  ALCARAZ Marc (aka eKameleon) <vegas@ekameleon.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2007
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
 */
import andromeda.controller.AbstractController;
import andromeda.model.ModelCollector;

import asgard.config.Config;
import asgard.events.RemotingEvent;

import egallery.events.EventList;
import egallery.model.ModelList;
import egallery.model.PictureModel;
import egallery.net.remoting.ApplicationService;
import egallery.vo.PictureVO;

import vegas.events.Delegate;
import vegas.events.Event;

/**
 * This controller is invoqued to receive all PictureVO from the php server.
 * @author eKameleon
 */
class egallery.controller.net.remoting.GetGallery extends AbstractController 
{
	
	/**
	 * Creates a new GetGallery instance.
	 */
	public function GetGallery() 
	{
		super();
		PictureVO.register() ;
	}

	/**
	 * Handles the event.
	 */
	/*override*/ public function handleEvent(e:Event) 
	{
		
		getLogger().info( this + " handleEvent.") ;
		
		var model:PictureModel = PictureModel( ModelCollector.get( ModelList.PICTURE_MODEL ) ) ;
		if (model.size() > 0)
		{
			model.clear() ;
		} 
		var methodName:String  = ApplicationService.getConfigMethodName  ( EventList.GET_GALLERY ) ;
		var serviceName:String = ApplicationService.getConfigServiceName ( EventList.GET_GALLERY ) ;
			
		var service:ApplicationService = new ApplicationService() ;

		service.addEventListener( RemotingEvent.RESULT , new Delegate(this, onResult) ) ;
		service.gatewayUrl  = ApplicationService.getConfigGatewayUrl() ;
		service.serviceName = serviceName ;
		service.methodName  = methodName ;
		service.params      = null ;
			
		service.trigger() ;
		
	}

	/**
	 * Invoqued if the Remoting connection is success.
	 */
	public function onResult( e:RemotingEvent ):Void
	{
		
		var result = e.getResult() ;
		
		if (result == null)
		{
			getLogger().warn( this + " run failed with an empty result.") ;
		}
		else
		{
			
			var size:Number = result.length ;
			getLogger().info( this + " run " + size + " PictureVO objects" ) ;
			if (size > 0)
			{
				var vo:PictureVO ;
				var model:PictureModel = PictureModel( ModelCollector.get( ModelList.PICTURE_MODEL ) ) ;
				var path:String = Config.getInstance().libraryPath || "" ;
		
				for (var i:Number = 0 ; i< size ; i++)
				{
					vo = result[i] ;
					vo.url = path + vo.url ;
					model.addVO( vo ) ;
					
				}
		
				model.run() ;
		
			}
			else
			{
				getLogger().warn( this + " no picture found.") ;
			}
			
		}

	}
}